var CONST_YES = "YES";
var CONST_FAIL = "NO";
var CONST_POPUP = "#pbox";
var CONST_POPUP_TITLE = "#cptitle";
var CONST_POPUP_CLOSE = "#pclose a";
var CONST_POPUP_CONTENT = "#cpcontent";
var CONST_ERROR = "1";
var months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];

var isPreventing = false;
var LGCMS = {
    jQuery: $,
    updateGridView: function() {
	$.fn.yiiGridView.update('DataGrid', {
	    data: $(this).serialize()
	});
    },
    ajaxCallback: function(url, callback) {
	if (isPreventing === true) {
	    return false;

	}

	isPreventing = true;
	setTimeout(function() {
	    isPreventing = false;
	}, 500);

	LGCMS.loading();
	$('#system-message').css('display', 'none');
	var result = 'System Error';
	$.ajax({
	    async: false,
	    cache: false,
	    type: "GET",
	    dataType: "json",
	    timeout: 1000,
	    url: url,
	    success: function(data) {
		/*Minh.Mai: Add following condition to make sure that the session hasn't ended.*/
		if (data !== null && data.hasOwnProperty("expired"))
		    window.location = data.expired;

		switch (callback)
		{
		    default:
			LGCMS.updateGridView();
			if (data != null)
			{
			    $('#message').html(data.content);
			    $('#message').attr('class', 'flash-' + data.status);
			    $('#system-message').css('display', 'block');
			}
			break;
		}
	    }
	});
	LGCMS.completed();
    },
    ajax: function(url) {
	LGCMS.loading();
	var result = 'System Error';
	$.ajax({
	    async: false,
	    cache: false,
	    type: "GET",
	    dataType: "html",
	    timeout: 1000,
	    url: url,
	    success: function(msg) {
		result = msg;
	    }
	});
	LGCMS.completed();
	return result;
    },
    ajaxCall: function(url, dataType) {

	if (isPreventing === true) {
	    return false;
	}

	isPreventing = true;
	setTimeout(function() {
	    isPreventing = false;
	}, 500);

	LGCMS.loading();
	var result = 'System Error';
	$.ajax({
	    async: false,
	    cache: false,
	    type: "GET",
	    dataType: dataType,
	    timeout: 1000,
	    url: url,
	    success: function(msg) {
		result = msg;
	    }
	});
	LGCMS.completed();
	return result;
    },
    ajaxPost: function(url, data, callback) {
	if (isPreventing === true) {
	    return false;

	}

	isPreventing = true;
	setTimeout(function() {
	    isPreventing = false;
	}, 500);
	
	LGCMS.loading();
	$('#system-message').css('display', 'none');
	var result = 'System Error';
	$.ajax({
	    async: false,
	    cache: false,
	    type: "POST",
	    data: data,
	    dataType: "json",
	    timeout: 1000,
	    url: url,
	    success: function(data) {
		/*Minh.Mai: Add following condition to make sure that the session hasn't ended.*/
		if (data !== null && data.hasOwnProperty("expired"))
		    window.location = data.expired;

		switch (callback)
		{
		    default:
			LGCMS.updateGridView();
			if (data != null)
			{
			    $('#message').html(data.content);
			    $('#message').attr('class', 'flash-' + data.status);
			    $('#system-message').css('display', 'block');
			}
			break;
		}
	    }
	});
	LGCMS.completed();
    },
    loading: function() {
	if ($("#LGCMSLoading") != undefined) {
	    $("#LGCMSLoading").remove();
	}
	var top = ($(window).height() - 30) / 2 + 'px';
	var div = document.createElement('div');
	$(div).attr("id", "LGCMSLoading");
	$(div).css("top", top);
	$(div).css("z-index", "9999");
	$(div).html("Loading...");
	$("body").append(div);
    },
    completed: function() {
	$("#LGCMSLoading").fadeOut(1000, function() {
	    $("#LGCMSLoading").remove();
	});
    },
    addTooltip: function() {
	$(document).ready(function() {
	    $("div.errorMessage").tooltip({
		bodyHandler: function() {
		    return $(this).html();
		}
	    });
	    $("div.inforMessage").tooltip({
		bodyHandler: function() {
		    return $(this).html();
		}
	    });
	});
    },
    //Set task for save(s) actions
    setTask: function(frmId, task) {

	if (jQuery(frmId + " #cmsTask").length == 0)
	{
	    //append cmsTask to form
	    var input = document.createElement('input');
	    $(input).attr("id", "cmsTask");
	    $(input).attr("name", "cmsTask");
	    $(input).attr("type", "hidden");
	    $(input).attr("value", task);

	    $(frmId).append(input);
	}


	if (isPreventing === false) {
	    $(frmId).submit();
	    isPreventing = true;
	    setTimeout(function() {
		isPreventing = false;
	    }, 500);


	}
    },
    //This function is to check that input string is a valid json string or not
    isJsonString: function(str) {
	try {
	    $.parseJSON(str);
	} catch (e) {
	    return false;
	}
	return true;
    },
    //get the next div element from current element, show it as a pop up
    showPopup: function() {
	$('.showPopup').live('click', function() {
	    var width = $(this).attr('dialog-width') !== '' ? $(this).attr('dialog-width') : 300;
	    var height = $(this).attr('dialog-height') !== '' ? $(this).attr('dialog-height') : 400;
	    var title = $(this).attr('dialog-title') !== '' ? $(this).attr('dialog-title') : 'Lotus Ocean';
	    var content = $(this).next('div').html();
	    var div = document.createElement('div');
	    $(div).html(content);
	    $(div).dialog({title: title, width: width, height: height, resizable: false, modal: true}).dialog('open');
	});

    },
    getGridViewIdonPopup: function() {
	return $("#result").children().eq(1).attr("id");
    },
    
    kAlert: function(title, content, width, height) {    	
    	var div;
    	if($('#kalert').length <= 0)
		{
    		div = document.createElement('div');
    		$(div).attr('id','kalert');
		}
    	else
		{
    		div = $('#kalert');
		}
    	$(div).html(content);
    	$(div).dialog({
			modal: true,
			height: height,
			width: width,
			resizable: false,				
			title: title
		}).dialog('open');
    },
    kAlertClose: function()
    {
    	$("#kalert").dialog('close');
    }
}
